//
//  LeaveWordsTableViewCell.m
//  RainbowFM
//
//  Created by Kratos on 14/12/14.
//  Copyright (c) 2014年 RB. All rights reserved.
//

#import "LeaveWordsTableViewCell.h"

#import "NSString+Size.h"

#import "CompereModel.h"

#define DEINE_H 70.f

#define Cntent_Image_tag 1001
#define Cntent_Voice_tag 1002
#define Cntent_Text_tag 1003

#define ContentViewDefineSize CGSizeMake(81.f,43.f)
#define ContentTextViewDefineSize CGSizeMake(55.f,22.f)

@implementation LeaveWordsTableViewCell

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


-(void)setType:(LeaveWordsType)type
{
    _type = type;
    
    if (_contentsView) {
        [_contentsView removeFromSuperview];
        _contentsView = nil;
    }
    
    _contentsView = [[UIView alloc]initWithFrame:CGRectMake(self.part == InterlocutionPartOfCompere ? 10.f : CGRectGetWidth(self.contentView.bounds) - 10.f, 20.f, ContentViewDefineSize.width, ContentViewDefineSize.height)];
    
    UIImageView *background = [[UIImageView alloc]initWithFrame:_contentsView.bounds];
    UIEdgeInsets es = {22.f, 40.f, 22.f, 40.0f};
    [background setImage:[[UIImage imageNamed:self.part == InterlocutionPartOfCompere ? @"speak_l" : @"speak_r"]resizableImageWithCapInsets:es]];
    background.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin| UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin |UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [_contentsView addSubview:background];
    
    [_contentsView setBackgroundColor:[UIColor clearColor]];
    
    if (self.part == InterlocutionPartOfCompere ) {
        _contentsView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
    }
    else
    {
        _contentsView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
    }
    
    [self.contentView addSubview:_contentsView];
    
    for (UIView *view in [_contentsView subviews]) {
        if (view.tag == Cntent_Image_tag || view.tag == Cntent_Voice_tag || view.tag == Cntent_Text_tag ) {
            [view removeFromSuperview];
        }
    }
    
    //还原大小
    CGRect frame = _contentsView.frame;
    frame.size = ContentViewDefineSize;
    _contentsView.frame = frame;
    
    switch (type) {
        case LeaveWordsTypeOfText:
        {
            _contentTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.part == InterlocutionPartOfCompere ? 20.f : 5.f, (CGRectGetHeight(frame) - ContentTextViewDefineSize.height)/2, ContentTextViewDefineSize.width, ContentTextViewDefineSize.height)];
            _contentTextLabel.tag = Cntent_Text_tag;
            _contentTextLabel.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleHeight;
            [_contentTextLabel setFont:[UIFont systemFontOfSize:12]];
            [_contentTextLabel setNumberOfLines:0];
            [_contentTextLabel setLineBreakMode:NSLineBreakByCharWrapping];
            [_contentTextLabel setTextAlignment:self.part == InterlocutionPartOfCompere ? NSTextAlignmentLeft : NSTextAlignmentRight];
            [_contentsView addSubview:_contentTextLabel];
        }
            break;
        case LeaveWordsTypeOfImage:
        {
            _contentImageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.part == InterlocutionPartOfCompere ? 20.f : 10.f, 10.f, 50.f, 70.f)];
            _contentImageView.tag = Cntent_Image_tag;
            [_contentImageView setBackgroundColor:[UIColor lightGrayColor]];
            
            frame.size = CGSizeMake(80.f, 90.f);
            if (self.part == InterlocutionPartOfCompere) {
                
            }else{
                frame.origin.x = CGRectGetWidth(self.contentView.bounds) - frame.size.width - 10.f;
                
            }
            
            _contentsView.frame = frame;
            
            [_contentsView addSubview:_contentImageView];
        }
            break;
        case LeaveWordsTypeOfVoice:
        {
            _contentVoiceView = [[[NSBundle mainBundle] loadNibNamed:self.part == InterlocutionPartOfCompere ? @"VoiceViewL":@"VoiceViewR" owner:nil options:nil]lastObject];
            _contentVoiceView.tag = Cntent_Voice_tag;
            
            typeof(self) self_ = self;
            dispatch_async(dispatch_get_main_queue(), ^{
                [self_.contentVoiceView setImagesByDirection:self_.part == InterlocutionPartOfCompere ? 0:1];
            });
            
            CGRect voiceViewFrame = _contentVoiceView.frame;
            
            frame.size.width = 110.f;
            voiceViewFrame.origin.y = (ContentViewDefineSize.height - CGRectGetHeight(_contentVoiceView.bounds))/2;
            if (self.part == InterlocutionPartOfCompere) {
                voiceViewFrame.origin.x = 20.f;
            }else{
                voiceViewFrame.origin.x = 10.f;
                frame.origin.x = CGRectGetWidth(self.contentView.bounds) - frame.size.width - 10.f;
                
            }
            _contentsView.frame = frame;
            _contentVoiceView.frame = voiceViewFrame;
            
            [_contentsView addSubview:_contentVoiceView];

        }
            break;
        default:
            break;
    }
}

-(void)updateContentsWithItem:(LeaveWordItem *)item
{
    [_nameLabel setText:item.cmtUserName];
    [_timeLabel setText:item.cmtDt];
    
    if (_type != item.type) {
        [self setType:item.type];
    }
    
    NSString *contentStr = nil;
    switch (_type) {
        case LeaveWordsTypeOfText:
        {
            contentStr = item.cmtContent;
        }
            break;
        case LeaveWordsTypeOfImage:
        {
            contentStr = item.picUrl;
        }
            break;
        case LeaveWordsTypeOfVoice:
        {
            contentStr = item.soundUrl;
        }
            break;
        default:
            break;
    }
    
    [self setContent:contentStr];
}

-(void)setContent:(NSString *)contentStr
{
    _contentStr = contentStr;
    switch (_type) {
        case LeaveWordsTypeOfText:
        {
            [self setContentText:contentStr];
        }
            break;
        case LeaveWordsTypeOfImage:
        {
            [self setContentImagePath:contentStr];
        }
            break;
        case LeaveWordsTypeOfVoice:
        {
            [self setContentVoicePath:contentStr];
        }
            break;
        default:
            break;
    }

}

/**
 * 文字内容
 * @param contentStr
 * @return
 * @author Kratos
 */
-(void)setContentText:(NSString *)contentStr
{
    [_contentTextLabel setText:contentStr];
    
    __block CGRect frame = _contentsView.frame;
    __block CGRect labelFrame = _contentTextLabel.frame;
    
    CGFloat changedH = 0.f;
    CGFloat changedW = 0.f;
    if (contentStr && contentStr.length > 0) {
        CGSize calculateSize = [contentStr calculateSize:CGSizeMake(250.f, 1000.f) font:_contentTextLabel.font];
        changedH = calculateSize.height > ContentTextViewDefineSize.height ? calculateSize.height - ContentTextViewDefineSize.height : 0;
        changedW = calculateSize.width > ContentTextViewDefineSize.width ? calculateSize.width - ContentTextViewDefineSize.width : 0;
    }
    
    
    frame.size.height = ContentViewDefineSize.height + changedH;
    frame.size.width = ContentViewDefineSize.width + changedW;
    
    labelFrame.size.height = ContentTextViewDefineSize.height + changedH;
    labelFrame.size.width = ContentTextViewDefineSize.width + changedW;
    
    dispatch_async(dispatch_get_main_queue(), ^{
        if (self.part == InterlocutionPartOfCompere) {
            
        }else{
            frame.origin.x = CGRectGetWidth(self.contentView.bounds) - frame.size.width - 10.f;
            
        }
        
        _contentsView.frame = frame;
        _contentTextLabel.frame = labelFrame;
    });
    
}

/**
 * 图片内容
 * @param contentStr
 * @return
 * @author Kratos
 */
-(void)setContentImagePath:(NSString *)contentStr
{
    [_contentImageView sd_setImageWithURL:[NSURL URLWithString:contentStr]];
}

/**
 * 语音内容
 * @param contentStr
 * @return
 * @author Kratos
 */
-(void)setContentVoicePath:(NSString *)contentStr
{
    
}

-(void)startVoice
{
    typeof(self) self_ = self;
    dispatch_async(dispatch_get_main_queue(), ^{
        if (self_.contentVoiceView) {
            [self_.contentVoiceView start];
        }
    });
    
}
-(void)stopVoice
{
    typeof(self) self_ = self;
    dispatch_async(dispatch_get_main_queue(), ^{
        if (self_.contentVoiceView) {
            [self_.contentVoiceView stop];
        }
    });
    
}

+(CGFloat)cellHightByText:(NSString *)text
{
    CGFloat changedH = 0.f;
    if (text && text.length > 0) {
        CGSize calculateSize = [text calculateSize:CGSizeMake(250.f, 1000.f) font:[UIFont systemFontOfSize:12]];
        changedH = calculateSize.height > ContentTextViewDefineSize.height ? calculateSize.height - ContentTextViewDefineSize.height : 0;
    }
    
    return DEINE_H + changedH;

}

+(CGFloat)cellHightByImage
{
    return 120.f;
}

+(CGFloat)cellHightByVoice
{
    return DEINE_H;
}

@end
